// 单剑特效 - p5.js
let sword;
let sword2;
let rotation = 0;
let glowIntensity = 0;
let isGlowing = false;

function setup() {
  createCanvas(800, 600, WEBGL);
  smooth();
  
  // 创建单剑
  sword = new Sword(0, 0);
  sword2 = new Sword(50, 50);
}

function draw() {
  background(0);
  
  // 旋转视角
  rotateX(PI/6);
  rotateY(frameCount * 0.01);
  
  // 绘制剑
  sword.update();
  sword.display();
  sword2.update();
  sword2.display();
  
  // 处理发光效果
  if (isGlowing) {
    glowIntensity += 0.1;
    if (glowIntensity > 5) {
      glowIntensity = 0;
      isGlowing = false;
    }
  }
}

class Sword {
  constructor(x, y) {
    this.position = createVector(x, y);
    this.length = 120;
    this.bladeWidth = 5;
    this.handleLength = 30;
    this.guardWidth = 20;
    this.swordColor = color(200, 200, 255);
    this.glowColor = color(0, 150, 255);
  }
  
  update() {
    // 剑身轻微浮动
    this.position.y = sin(frameCount * 0.05) * 5;
  }
  
  display() {
    push();
    translate(this.position.x, this.position.y);
    
    // 绘制剑柄
    this.drawHandle();
    
    // 绘制护手
    this.drawGuard();
    
    // 绘制剑身
    this.drawBlade();
    
    // 绘制剑尖光芒
    if (isGlowing) {
      this.drawGlow();
    }
    
    pop();
  }
  
  drawHandle() {
    push();
    translate(0, this.handleLength/2 + 5);
    fill(139, 69, 19); // 棕色剑柄
    noStroke();
    cylinder(3, this.handleLength);
    pop();
  }
  
  drawGuard() {
    push();
    translate(0, 5);
    fill(192, 192, 192); // 银色护手
    noStroke();
    box(this.guardWidth, 4, 4);
    pop();
  }
  
  drawBlade() {
    push();
    translate(0, -this.length/2 + 5);
    
    // 剑身
    fill(220, 220, 240);
    noStroke();
    box(this.bladeWidth, this.length, 2);
    
    // 剑刃高光
    stroke(255);
    strokeWeight(1);
    line(-this.bladeWidth/2 + 1, -this.length/2, -this.bladeWidth/2 + 1, this.length/2);
    line(this.bladeWidth/2 - 1, -this.length/2, this.bladeWidth/2 - 1, this.length/2);
    
    pop();
  }
  
  drawGlow() {
    push();
    translate(0, -this.length + 5);
    
    // 剑尖光芒
    stroke(this.glowColor);
    strokeWeight(glowIntensity);
    for (let i = 0; i < 8; i++) {
      let angle = TWO_PI * i / 8;
      let len = random(20, 40);
      line(0, 0, cos(angle) * len, sin(angle) * len - len);
    }
    
    // 能量球
    fill(this.glowColor);
    noStroke();
    sphere(glowIntensity * 2);
    
    pop();
  }
}

function mousePressed() {
  // 点击时触发发光效果
  isGlowing = true;
  glowIntensity = 0;
}

function keyPressed() {
  // 按空格键重置
  if (key === ' ') {
    isGlowing = false;
    glowIntensity = 0;
  }
}